<?php
/**
 * Trieda na tvorbu odkazov
 * @author Tomas Srna
 * @package bakalarka
 */
 
class Link
{
	private $title, $controller, $action, $class, $resource;
	private $submenu = NULL;
	
	/**
	 * Konstruktor
	 * @param string $title			Nazov odkazu
	 * @param string $controller	Controller
	 * @param string $action		Action
	 * @param string $class			Trieda HTML elementu <a>
	 */
	public function __construct($title, $resource, $controller, 
		$action = 'index', Menu $submenu = NULL, $class = "")
	{
		$this->title = $title;
		$this->controller = $controller;
		$this->action = $action;
		$this->class = $class;
		$this->submenu = $submenu;
		$this->resource = $resource;
	}
	
	/**
	 * Zisti ci mame pristup
	 * @return bool		Mame pristup?
	 */
	public function allowed()
	{
		if(!Db::hasInstance())
			return false;
		return Acl::instance()->allowed($this->resource);
	}
	
	/**
	 * Konverzia triedy na retazec
	 * @return string				Odkaz v HTML kode
	 */
	public function __toString()
	{
		return "<a href=\"" . APPBASE . 
			($this->controller == "index" ? "" : $this->controller) . 
			($this->action == "index" ? "" : "/".$this->action) . 
			"\"" . ($this->class == "" ? "" : " class=\"".$this->class.
			"\" ") . ">" . $this->title . "</a>".($this->submenu ? 
				"\n".$this->submenu : "");
	}
} 
